#include <iostream>
#include "scanner.h"
#include "lexer.h"
#include "symtab.h"
#include "parser.h"
#include "asm.h"
#define SECTSIZE   512
#define ELFHDR     0x10000// scratch space
typedef unsigned int uint32_t;


int main() {
    cout<< "编译开始:"<<endl;
    string src = "test/struct.q";
    FILE *file = fopen(src.c_str(), "r");
    Scanner scan(file);
    Lexer lexer(scan);
    Symtab symtab;
    //打印中间代码
    string srcEnd = src.substr(src.length() - 1, src.length());
    if (srcEnd!= "q") {
        cout << "源文件格式错误，必须以.q结尾" <<endl;
        return 0;
    }
    string srcPrefix = src.substr(0, src.length()-1);
    cout << " -------- start generate intermediate code --------" << endl;
    string mid = srcPrefix + "mq";
    remove(mid.c_str());
    FILE *midf = fopen(mid.c_str(), "a+");
    OrderGenerator generator(symtab, midf);
    Parser parser(lexer, symtab, generator);
    parser.process();
    generator.genMid();

    cout << " -------- start generate asm code --------" << endl;
    string tar = srcPrefix +"asm";
    remove(tar.c_str());
    FILE *target = fopen(tar.c_str(), "w");
    AsmGenerator asmGenerator(symtab,target);
    asmGenerator.genAsm();
    cout<<"编译结束"<<endl;
}
